using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;

public class VActBehaviour : MonoBehaviour
{
    //有可能这个才是最顶级的入口
    //纯属性了+封装
    public VActProcessor processor => GetComponent<VActProcessor>();
    public VActStateBase actState => GetComponent<VActState_Loader>().overrideState;
    public Vector3 PlayerCamForward(float dis = 0) => player.position + player.forward * dis;
    public int state = 0;

    public Transform player;

    private void Start()
    {
        player = GameObject.FindWithTag("Player").transform; //应该以XR Origin (XR Rig)为原点，如果以Cam则会定位飘到2楼
    }
    public void WalkToPoint(Vector3 position)
    {
        //在到达目标之前一直播放
        var agent = processor.mvm.agent;
        //这里很有可能需要处理时延
        agent.SetDestination(position);
        FuncObserver observer = new FuncObserver()
        {
            complete = () =>
            {
                return agent.remainingDistance <= agent.stoppingDistance; //closeEnough
            },
            tick = () =>
            {
                processor.ContinueState(actState.NameTo("Walk"));//播放走路动画
            },
            end = () =>
            {
                processor.ContinueState();//播放走路动画
            }
        };
        0.1f.CreateDelay().Link(observer).Subscribe();
    }

    public void ComeBack()
    {
        var agent = processor.mvm.agent;
        //这里很有可能需要处理时延
        agent.SetDestination(PlayerCamForward(1.5f));
        FuncObserver observer = new FuncObserver()
        {
            complete = () =>
            {
                return agent.remainingDistance <= agent.stoppingDistance; //closeEnough
            },
            tick = () =>
            {
                processor.ContinueState(actState.NameTo("Walk"));//播放走路动画
            },
            end = () =>
            {
                processor.ContinueState();//播放走路动画
                TurnToPoint(PlayerCamForward());
            }
        };
        0.1f.CreateDelay().Link(observer).Subscribe();
    }

    public void TurnToPoint(Vector3 targetPosition, float rotateSpeed = 1f)
    {
        TurnToAngle(GetRotationToTarget(targetPosition), rotateSpeed);
    }

    public void TurnToAngle(float angle, float rotateSpeed = 1)
    {
        Quaternion startRotation = transform.rotation;
        Quaternion targetRotation = startRotation * Quaternion.Euler(0, angle, 0);
        float duration = (Mathf.Abs(angle) / 180) / rotateSpeed; // 假设旋转的总时间为1秒钟
        float startTime = Time.time;
        TObserver turnObserver = new TObserver()
        {
            duration = duration,
            tickType = 1,
            tick = () =>
            {
                float ratio = (Time.time - startTime) / duration;
                transform.rotation = Quaternion.Slerp(startRotation, targetRotation, ratio);
            },
            end = () => transform.rotation = targetRotation
        }.Subscribe();
    }

    public void Show(string stateName,bool trigger)
    {
        if (trigger)
        {
            processor.TriggerState(actState.NameTo(stateName));
        }
        else
        {
            processor.ContinueState(actState.NameTo(stateName));
        }

    }

    public float GetRotationToTarget(Vector3 targetPosition)
    {
        Vector3 toTarget = (targetPosition - transform.position).normalized;
        toTarget.y = 0;

        Vector3 forward = transform.forward.normalized;
        forward.y = 0;

        float angle = Vector3.Angle(forward, toTarget);
        Vector3 cross = Vector3.Cross(forward, toTarget);

        // 判断旋转方向并将角度归一化到-180到180度之间
        return cross.y > 0 ? angle : -angle;
    }
}


